package com.dq.service.record.impl;

import com.dq.controller.in.query.ConsumeQuery;
import com.dq.domain.record.ProfitRecord;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.repository.AllRepository;
import com.dq.service.ShiroService;
import com.dq.service.record.ConsumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2019/12/10 9:55
 */
@Service
public class ConsumeServiceImpl extends AllRepository implements ConsumeService {
    @PersistenceContext
    private EntityManager entityManager;
    @Autowired
    private ShiroService shiroService;

    @Override
    public List<ProfitRecord> findByWhere(ConsumeQuery consumeQuery) {
        StringBuilder hql = new StringBuilder("select u from ProfitRecord u where u.deleted=false");
        Query q = createWhereHql(consumeQuery,hql);
        q.setFirstResult((consumeQuery.getPage()-1)*consumeQuery.getSize());
        q.setMaxResults(consumeQuery.getSize());
        List<ProfitRecord> list = q.getResultList();
        return list;
    }

    @Override
    public Long countByWhere(ConsumeQuery consumeQuery) {
        StringBuilder hql = new StringBuilder("select count(u) from ProfitRecord u where u.deleted=false");
        Query q = createWhereHql(consumeQuery,hql);
        return new Long((long) q.getResultList().get(0));
    }
    public Query createWhereHql(ConsumeQuery consumeQuery, StringBuilder hql) {
        SysUser user = shiroService.getUser();
        WxUser wxUser = dao.get(WxUser.class,consumeQuery.getUserId());
            hql.append(" and u.user=:user");
        if(consumeQuery.getChangeProfitType() != null){
            hql.append(" and u.changeProfitType=:changeProfitType");
        }
        if(consumeQuery.getProfitChangeType() != null){
            hql.append(" and u.profitChangeType=:profitChangeType");
        }
        if(consumeQuery.getBeginTime() != null){
            hql.append(" and u.createTime >=:beginTime");
        }
        if(consumeQuery.getEndTime() != null){
            hql.append(" and u.createTime <=:endTime");
        }
        hql.append(" order By u.createTime desc");
        Query query = entityManager.createQuery(hql.toString());
        query.setParameter("user",wxUser);
        if(consumeQuery.getChangeProfitType() != null){
            query.setParameter("changeProfitType",consumeQuery.getChangeProfitType());
        }
        if(consumeQuery.getProfitChangeType() != null){
            query.setParameter("profitChangeType",consumeQuery.getProfitChangeType());
        }
        if(consumeQuery.getBeginTime() != null){
            query.setParameter("beginTime",consumeQuery.getBeginTime());
        }
        if(consumeQuery.getEndTime() != null){
            query.setParameter("endTime",consumeQuery.getEndTime());
        }
        return query;
    }
}
